home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / Hacking & Misc / bundle of exploits.sit / bundle of exploits / jakal.c < prev    next >
C/C++ Source or Header  |  1998-07-17  |  10KB  |  467 lines

  1. /* Jackal - Stealth/FireWall scanner. With the use of halfopen ports
  2.    and sending SYNC (sometimes additional flags like FIN) one can
  3.    scan behind a firewall. And it shouldnt let the site feel we're scanning
  4.    by not doing a 3-way-handshake we hope to avoid any tcp-logging.
  5.    Credits: Halflife, Jeff (Phiji) Fay, Abdullah Marafie.
  6.    Alpha Tester: Walter Kopecky.
  7.    Results:
  8.    Some firewalls did allow SYN | FIN to pass through. No Site has
  9.    been able to log the connections though.. during alpha testing.
  10.    ShadowS shadows@kuwait.net
  11.    Copyleft (hack it i realy dont care).
  12.    */
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <signal.h>
  17. #include <string.h>
  18. #include <unistd.h>
  19. #include <netinet/in.h>
  20. #include <sys/socket.h>
  21. #include <arpa/inet.h>
  22. #include <linux/ip.h>
  23. #include <linux/tcp.h>
  24.  
  25. int timeout = 0;
  26. int counter = 0;
  27. void usage(char *name)
  28. {
  29.   printf("usage:%s [-h target_ip] [-i your_ip] [-t timeout] [-s start-port] [-e end-port] [-f FIN] [-a ACK]\n",name);
  30.   exit(-1);
  31. }
  32.  
  33. void handler(int whateva)
  34. {
  35.    alarm(0);
  36.    timeout = 1;
  37. }
  38. /* Checksum Gen */
  39.  
  40. unsigned short in_cksum(unsigned short *ptr, int nbytes)
  41. {
  42.     register long        sum;        /* assumes long == 32 bits */
  43.     u_short            oddbyte;
  44.     register u_short    answer;        /* assumes u_short == 16 bits */
  45.  
  46.     /*
  47.      * Our algorithm is simple, using a 32-bit accumulator (sum),
  48.      * we add sequential 16-bit words to it, and at the end, fold back
  49.      * all the carry bits from the top 16 bits into the lower 16 bits.
  50.      */
  51.  
  52.     sum = 0;
  53.     while (nbytes > 1)  {
  54.         sum += *ptr++;
  55.         nbytes -= 2;
  56.     }
  57.  
  58.                 /* mop up an odd byte, if necessary */
  59.     if (nbytes == 1) {
  60.         oddbyte = 0;        /* make sure top half is zero */
  61.         *((u_char *) &oddbyte) = *(u_char *)ptr;   /* one byte only */
  62.         sum += oddbyte;
  63.     }
  64.  
  65.     /*
  66.      * Add back carry outs from top 16 bits to low 16 bits.
  67.      */
  68.  
  69.     sum  = (sum >> 16) + (sum & 0xffff);    /* add high-16 to low-16 */
  70.     sum += (sum >> 16);            /* add carry */
  71.     answer = ~sum;        /* ones-complement, then truncate to 16 bits */
  72.     return(answer);
  73. }
  74.  
  75. /* Generates the tcp header and packet */
  76.  
  77. struct tcphdr packetgen(int fin,int ack,unsigned int src_addr,unsigned int dst_addr, int port)
  78. {
  79.   /* we create both a tcpheader and a ipheader to calculate checksum */
  80.  
  81.   struct tcphdr packet;
  82.   
  83.   struct ipheader
  84.     {
  85.       unsigned int source_address;
  86.       unsigned int dest_address;
  87.       unsigned char placeholder;
  88.       unsigned char protocol;
  89.       unsigned short tcp_length;
  90.       struct tcphdr tcp;
  91.    }
  92.     pseudo_header;
  93.     
  94.     
  95.  
  96.     counter++;
  97.  
  98.     /* Fill the headers with the options and data */
  99.  
  100.     packet.source = getpid() + counter;
  101.     packet.dest = htons(port);
  102.     packet.seq = getpid() + counter;
  103.     packet.ack_seq = 0;
  104.     packet.res1 = 0;
  105.     packet.doff = 5;
  106.     packet.res2 = 0;
  107.     packet.fin = fin;
  108.     packet.syn = 1;
  109.     packet.rst = 0;
  110.     packet.psh = 0;
  111.     packet.ack = ack;
  112.     packet.urg = 0;
  113.     packet.window = htons(512);
  114.     packet.check = 0;
  115.     packet.urg_ptr = 0;               
  116.  
  117.     /* We need this for the checksum */
  118.  
  119.     pseudo_header.source_address = src_addr;
  120.     pseudo_header.dest_address = dst_addr;
  121.     pseudo_header.placeholder = 0;
  122.     pseudo_header.protocol = IPPROTO_TCP;
  123.     pseudo_header.tcp_length = htons(20);
  124.     bcopy(&packet, &pseudo_header.tcp, 20);
  125.  
  126.     /* Get the checksum and viowlla tcphdr is done :) */
  127.  
  128.     packet.check = in_cksum((unsigned short *)&pseudo_header, 32);
  129.  
  130.     return packet;
  131. }
  132.  
  133. int scan(struct tcphdr packet,int port,unsigned int dst_addr)
  134. {
  135.   struct sockaddr_in remote;
  136.   int len;
  137.  
  138.   /* The header we'll receive the info in */
  139.  
  140.   struct rect
  141.   {
  142.       struct iphdr ip;
  143.       struct tcphdr tcp;
  144.       unsigned char blah[65535];
  145.   }
  146.     recv_tcp;
  147.     int sockd;
  148.  
  149.     /* Now we fill the sock struct */
  150.  
  151.   remote.sin_family = AF_INET;
  152.   remote.sin_port = htons(port);
  153.   remote.sin_addr.s_addr = dst_addr;
  154.  
  155.   len=sizeof(remote);
  156.  
  157.   signal(SIGALRM, handler);
  158.  
  159.   sockd = socket(AF_INET, SOCK_RAW, IPPROTO_TCP);
  160.  
  161.   if(sockd < 0)
  162.     {
  163.       perror("Socket");
  164.       exit(1);
  165.     }
  166.  
  167.  
  168.   /* Blast the packet into ablivion! */
  169.  
  170.   sendto(sockd, &packet, 20, 0, (struct sockaddr *)&remote, len);
  171.  
  172.   timeout = 0;
  173.   alarm(10);
  174.   while(1)
  175.     {
  176.       /* Now we sit and read */
  177.  
  178.       read(sockd, (struct recv_tcp *)&recv_tcp, 65535);
  179.       if(timeout == 1)
  180.     {
  181.       close(sockd);
  182.       timeout=0;
  183.       return -1;
  184.     }
  185.       if(recv_tcp.tcp.dest == (getpid() + counter))
  186.     {
  187.          alarm(0);
  188.          close(sockd);
  189.  
  190.      /* It shouldnt be 1 */
  191.  
  192.          if(recv_tcp.tcp.rst == 1)
  193.        return 0;
  194.          else
  195.        return 1;
  196.     }
  197.     }
  198. }
  199.  
  200.  
  201. main(int argc, char **argv)
  202. {
  203.   int c;
  204.   int start = 0;
  205.   int end = 0;
  206.   int fin = 0;
  207.   int ack = 0;
  208.   int port = 0;
  209.   int retval;
  210.  
  211.   struct tcphdr packet;
  212.   int host = 0;
  213.   int target = 0;
  214.  
  215.   char source[100];
  216.   char destination[100];
  217.  
  218.   if(getuid() != 0)
  219.     {
  220.       printf("Need root to run this I'm afraid .\n");
  221.       exit(-2);
  222.     }
  223.   if(argc < 2)
  224.     usage(argv[0]);
  225.   while ((c = getopt (argc, argv, "s:e:h:i:af")) != EOF) 
  226.     {
  227.       switch (c)
  228.     {
  229.       
  230.     case 's':
  231.       start = atoi(optarg);
  232.       break;
  233.     case 'e':
  234.       end = atoi(optarg);
  235.       break;
  236.     case 'a':
  237.       ack = 1;
  238.       break;
  239.     case 'f':
  240.       fin = 1;
  241.       break;
  242.     case 'h':
  243.       strcpy(destination,optarg);
  244.       target = 1;
  245.       break;
  246.     case 'i':
  247.       strcpy(source,optarg);
  248.       host = 1;
  249.       break;
  250.     case 't':
  251.       timeout = atoi(optarg);
  252.       break;
  253.     default:
  254.       usage(argv[0]);
  255.  
  256.     }
  257.     }
  258.   if((host == 0) || (target == 0))
  259.     usage(argv[0]);
  260.   if(start == 0)
  261.     port = start;
  262.   if(end == 0)
  263.     end = 1024;
  264.   for(;port < (end + 1);port++)
  265.     {
  266.  
  267.       packet = packetgen(fin,ack,inet_addr(source),inet_addr(destination),port);
  268.  
  269.       if( (retval = scan(packet,port,inet_addr(source))) ==  1)
  270.       printf("Port:%i is open.\n",port);
  271.     }
  272.   
  273.   exit(0);
  274. }
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286. ==================================================================================
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298. /**********************************************************************
  299. ** This is a real simple half opened connection scanner I
  300. ** put together. It scans ports 0 to 1024 for listening
  301. ** processes, and writes to stdout the ports that are 
  302. ** listening.
  303. **
  304. ** halflife@saturn.net
  305. **
  306. ** NOTE: You have to define MY_IP as your ip. If you have
  307. ** a dynamic ip, this is gonna bite :-).
  308. *********************************************************************/
  309.  
  310. /* define the following as your ip address */
  311. #define MY_IP        "193.62.1.250"
  312.  
  313. #include <stdio.h>
  314. #include <stdlib.h>
  315. #include <signal.h>
  316. #include <string.h>
  317. #include <unistd.h>
  318. #include <netinet/in.h>
  319. #include <sys/socket.h>
  320. #include <arpa/inet.h>
  321. #include <linux/ip.h>
  322. #include <linux/tcp.h>
  323.  
  324. int syn_timeout = 0;
  325.  
  326. unsigned short in_cksum(unsigned short *, int);
  327. int scan_port(unsigned short, unsigned int, unsigned int);
  328. void alarm_handler(int);
  329.  
  330. void alarm_handler(int s)
  331. {
  332.    alarm(0);
  333.    syn_timeout = 1;
  334. }
  335.  
  336. int scan_port(unsigned short port, unsigned int src_addr, unsigned int dst_addr)
  337. {
  338.    struct tcphdr send_tcp;
  339.    struct recv_tcp
  340.    {
  341.       struct iphdr ip;
  342.       struct tcphdr tcp;
  343.       unsigned char blah[65535];
  344.    }recv_tcp;
  345.    struct pseudo_header
  346.    {
  347.       unsigned int source_address;
  348.       unsigned int dest_address;
  349.       unsigned char placeholder;
  350.       unsigned char protocol;
  351.       unsigned short tcp_length;
  352.       struct tcphdr tcp;
  353.    }pseudo_header;
  354.    int tcp_socket;
  355.    struct sockaddr_in sin;
  356.    int sinlen;
  357.    static int blah = 0;
  358.    
  359.    blah++;
  360.    send_tcp.source = getpid() + blah;
  361.    send_tcp.dest = htons(port);
  362.    send_tcp.seq = getpid() + blah;
  363.    send_tcp.ack_seq = 0;
  364.    send_tcp.res1 = 0;
  365.    send_tcp.doff = 5;
  366.    send_tcp.res2 = 0;
  367.    send_tcp.fin = 0;
  368.    send_tcp.syn = 1;
  369.    send_tcp.rst = 0;
  370.    send_tcp.psh = 0;
  371.    send_tcp.ack = 0;
  372.    send_tcp.urg = 0;
  373.    send_tcp.window = htons(512);
  374.    send_tcp.check = 0;
  375.    send_tcp.urg_ptr = 0;               
  376.    pseudo_header.source_address = src_addr;
  377.    pseudo_header.dest_address = dst_addr;
  378.    pseudo_header.placeholder = 0;
  379.    pseudo_header.protocol = IPPROTO_TCP;
  380.    pseudo_header.tcp_length = htons(20);
  381.    bcopy(&send_tcp, &pseudo_header.tcp, 20);
  382.    send_tcp.check = in_cksum((unsigned short *)&pseudo_header, 32);
  383.    sin.sin_family = AF_INET;
  384.    sin.sin_port = htons(port);
  385.    sin.sin_addr.s_addr = dst_addr;
  386.    sinlen=sizeof(sin);
  387.    signal(SIGALRM, alarm_handler);
  388.    tcp_socket = socket(AF_INET, SOCK_RAW, IPPROTO_TCP);
  389.    if(tcp_socket < 0)
  390.    {
  391.       fprintf(stderr, "couldnt open raw socket\n");
  392.       exit(1);
  393.    }
  394.    sendto(tcp_socket, &send_tcp, 20, 0, (struct sockaddr *)&sin, sinlen);
  395.    syn_timeout = 0;
  396.    alarm(10);
  397.    while(1)
  398.    {
  399.       read(tcp_socket, (struct recv_tcp *)&recv_tcp, 65535);
  400.       if(syn_timeout == 1) {close(tcp_socket);syn_timeout=0;return -1;}
  401.       if(recv_tcp.tcp.dest == (getpid() + blah))
  402.       {
  403.          alarm(0);
  404.          close(tcp_socket);
  405.          if(recv_tcp.tcp.rst == 1) return 0;
  406.          else return 1;
  407.       }
  408.    }
  409. }
  410.  
  411. unsigned short in_cksum(unsigned short *ptr, int nbytes)
  412. {
  413.     register long        sum;        /* assumes long == 32 bits */
  414.     u_short            oddbyte;
  415.     register u_short    answer;        /* assumes u_short == 16 bits */
  416.  
  417.     /*
  418.      * Our algorithm is simple, using a 32-bit accumulator (sum),
  419.      * we add sequential 16-bit words to it, and at the end, fold back
  420.      * all the carry bits from the top 16 bits into the lower 16 bits.
  421.      */
  422.  
  423.     sum = 0;
  424.     while (nbytes > 1)  {
  425.         sum += *ptr++;
  426.         nbytes -= 2;
  427.     }
  428.  
  429.                 /* mop up an odd byte, if necessary */
  430.     if (nbytes == 1) {
  431.         oddbyte = 0;        /* make sure top half is zero */
  432.         *((u_char *) &oddbyte) = *(u_char *)ptr;   /* one byte only */
  433.         sum += oddbyte;
  434.     }
  435.  
  436.     /*
  437.      * Add back carry outs from top 16 bits to low 16 bits.
  438.      */
  439.  
  440.     sum  = (sum >> 16) + (sum & 0xffff);    /* add high-16 to low-16 */
  441.     sum += (sum >> 16);            /* add carry */
  442.     answer = ~sum;        /* ones-complement, then truncate to 16 bits */
  443.     return(answer);
  444. }
  445.  
  446. main(int argc, char **argv)
  447. {   
  448.    unsigned short i;
  449.    if(argc < 2)
  450.    {
  451.       fprintf(stderr, "%s target_ip\n", argv[0]);
  452.       exit(0);
  453.    }
  454.    if(geteuid() != 0)
  455.    {
  456.       fprintf(stderr, "this program requires root\n");
  457.       exit(0);
  458.    }
  459.    printf("Scanning %s\n", argv[1]);
  460.    for(i=0;i < 1025;i++)
  461.    {
  462.       if(scan_port(i, inet_addr(MY_IP), inet_addr(argv[1]))==1)
  463.          printf("Port %d active\n", i);
  464.    }
  465. }
  466.  
  467.